package com.ellricky.baselib.ui.visit;


import android.view.View;

import com.andr.common.http.callback.JOCallback;
import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.ui.mvp.BasePresenter;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.dialog.picker.EPickerEntityListener;
import com.ellricky.baselib.dialog.picker.EPickerUtil;
import com.ellricky.baselib.entity.IdNamePickerEntity;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.util.EAppUtil;
import com.ellricky.baselib.widget.EditItemView;
import com.flyco.roundview.RoundTextView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ellricky.baselib.http.Api.API_VISIT_SAVE;

public class VisitCreateActivity extends BaseToolBarActivity<Void, BasePresenter<Void>> {

    private EditItemView etWish;
    private EditItemView etCharacter;
    private EditItemView etLove;
    private EditItemView etSpecial;
    private EditItemView etRemark;
    private RoundTextView btnSubmit;

    private String clueId;

    private EPickerUtil ePickerUtil;
    private List<IdNamePickerEntity> wishList = new ArrayList<>();
    private IdNamePickerEntity selWish;

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_visit_create;
    }

    @Override
    protected void initUIView() {
        super.initUIView();
        mTvTitle.setText(R.string.base_visit_create);
        clueId = getIntent().getStringExtra(BaseConstant.KEY_ID);

        etWish = findViewById(R.id.etWish);
        etCharacter = findViewById(R.id.etCharacter);
        etLove = findViewById(R.id.etLove);
        etSpecial = findViewById(R.id.etSpecial);
        etRemark = findViewById(R.id.etRemark);
        btnSubmit = findViewById(R.id.btnSubmit);

        ePickerUtil = new EPickerUtil(this);
        wishList.add(new IdNamePickerEntity("1", "是"));
        wishList.add(new IdNamePickerEntity("2", "否"));
        wishList.add(new IdNamePickerEntity("3", "待考虑"));

        etWish.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == etWish) {
            ePickerUtil.showVisitWishPicker(wishList, selWish, new EPickerEntityListener<IdNamePickerEntity>() {
                @Override
                public void onSelectItem(int pickType, IdNamePickerEntity t) {
                    selWish = t;
                    etWish.setValue(t.getName());
                }
            });
            return;
        }

        if (v == btnSubmit) {
            postSubmit();
        }
    }


    private void postSubmit() {
        String wish = etWish.getValue();
        if (EmptyUtil.isEmpty(wish)) {
            ToastUtil.show(this, R.string.base_visit_wish_if_select);
            return;
        }

        String childNature = etCharacter.getValue();
        if (EmptyUtil.isEmpty(childNature)) {
            ToastUtil.show(this, R.string.base_visit_character_input);
            return;
        }

        String loveInfo = etLove.getValue();
        if (EmptyUtil.isEmpty(loveInfo)) {
            ToastUtil.show(this, R.string.base_visit_love_input);
            return;
        }

        String specialRequired = etSpecial.getValue();
        if (EmptyUtil.isEmpty(specialRequired)) {
            ToastUtil.show(this, R.string.base_visit_special_input);
            return;
        }

        String backInfo = etRemark.getValue();
        if (EmptyUtil.isEmpty(backInfo)) {
            ToastUtil.show(this, R.string.base_visit_remark_input);
            return;
        }


        String url = EAppUtil.getApiHost() + API_VISIT_SAVE;
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("backvisitId", clueId);
        paramMap.put("backAnswer", selWish.getId());
        paramMap.put("childNature", childNature);
        paramMap.put("loveInfo", loveInfo);
        paramMap.put("specialRequired", specialRequired);
        paramMap.put("backInfo", backInfo);

        HttpUtils.getInstance().post(url, paramMap, new JOCallback(this) {
            @Override
            protected void onSuccess(JSONObject jsonObject, String s) {
                setResult(BaseConstant.RESULT_CODE_ADD);
                finish();
            }
        });

    }


}
